home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / TUBESRC.ZIP / TUBE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-28  |  5.1 KB  |  263 lines

  1. /*************************************************************************
  2.     Program    : Tubular
  3.     File    : tube.cpp
  4.     Notes    : Set tab spaces to 4.
  5.     
  6.     Programmer    : James Johnson
  7.     Date        : 1-14-94
  8.     Version        : 1.0
  9.     
  10.     Comments    : This source has a few Visual C/C++ 'isms.  Anything 
  11.                   with an underscore are these 'isms.
  12.     
  13.  ************************************************************************/
  14.  
  15. #include <stdlib.h>
  16. #include <time.h>
  17. #include <conio.h>
  18. #include <math.h>
  19.  
  20. const int Max_Z      = 512;
  21. const int Min_Z      = 2;
  22. const int Max_X_org  = 300;
  23. const int Min_X_org  = 20;
  24. const int Max_Y_org  = 180;
  25. const int Min_Y_org  = 20;
  26. const int Max_Dz     = 64;
  27. const int Min_Dz     = 2;
  28. const int Factor     = 2;
  29. const int Def_DZ     = 8;
  30. const int Max_Rand   = 256;
  31. const int Step         = 20;
  32. const int Max_Active = Max_Z / 2 * Step;
  33. const float PI2         = 6.283185307;
  34.  
  35. #if Max_Rand <= 128
  36.     typedef struct{
  37.         int x;
  38.         int y;
  39.         int xc;
  40.         int yc;
  41.         int z;
  42.         unsigned Old_pos;
  43.     }Point;
  44. #else
  45.     typedef struct{
  46.         long x;
  47.         long y;
  48.         long xc;
  49.         int yc;
  50.         int z;
  51.         unsigned Old_pos;
  52.     }Point;
  53. #endif
  54.  
  55. Point star[Max_Active];
  56. int Xtable[Max_Rand];
  57. int Ytable[Max_Rand];
  58. int Index[Max_Rand];
  59.  
  60. void setmode13(void);
  61. void setmode03(void);
  62. void setpal(void);
  63. inline void waitvrt(void);
  64.  
  65. void main()
  66. {   
  67.     unsigned char _far *vidmem = (unsigned char _far *)0xA0000000;
  68.     int i, Y_tmp, X_tmp, k, Randnum,tmp;
  69.     int Num_Active            = 0;
  70.     int dz                    = Def_DZ;
  71.     unsigned Tube_Xr        = 60;
  72.     unsigned Tube_Yr        = 60;
  73.     unsigned X_org            = 160;
  74.     unsigned Y_org            = 100;
  75.     unsigned X_choice        = 0;
  76.     unsigned Y_choice        = 0;
  77.     unsigned char X_count    = 64;
  78.     unsigned char Y_count    = 0;
  79.     unsigned char D_tube_xr = 64;
  80.     unsigned char D_tube_yr = 0;
  81.     unsigned char count        = 0;
  82.     char quit                = 0;
  83.     
  84.     for(i=0; i < Max_Rand; i++){
  85.         Xtable[i] = (int)(128 * cos(i * PI2 / Max_Rand) + .5); 
  86.         Ytable[i] = (int)(128 * sin(i * PI2 / Max_Rand) + .5);
  87.             
  88.         Index[i]=i;
  89.     }
  90.     
  91.     srand((unsigned)time(NULL));
  92.     for(k=0; k < Max_Rand/2; k++)
  93.         for(i=0; i < Max_Rand; i++){
  94.             Randnum = rand() % Max_Rand;
  95.             tmp = Index[Randnum];
  96.             Index[Randnum] = Index[i];
  97.             Index[i] = tmp;
  98.         }
  99.             
  100.         
  101.     setmode13();
  102.     setpal();
  103.                 
  104.     while(!quit){
  105.     
  106.         // Fill in the Field.
  107.         for(k=0; k < Step; k++)
  108.             if(Num_Active < Max_Active){
  109.                 for(i=0; star[i].z; i++);
  110.                 star[i].x = Tube_Xr * Xtable[Index[X_choice]];
  111.                 star[i].y = Tube_Yr * Ytable[Index[Y_choice]];
  112.                 star[i].z = Max_Z;
  113.                 star[i].xc = 160 + (120 * Xtable[X_count]) / Max_Rand;
  114.                 star[i].yc = 100 + (80 * Ytable[Y_count]) / Max_Rand;
  115.                 
  116.                 if(++X_choice > Max_Rand)
  117.                     X_choice = 0;
  118.                 if(++Y_choice > Max_Rand)
  119.                     Y_choice = 0;
  120.                 
  121.                 Num_Active++;
  122.             }else
  123.                 break;    // No need to loop anymore.
  124.             
  125.         for(i=0; i < Max_Active; i++){
  126.             // If the Star is Active.
  127.             if(star[i].z){
  128.                 // Erase the old star.
  129.                 *(vidmem + star[i].Old_pos) = 0;
  130.                  
  131.                  if(star[i].z > Min_Z){
  132.                     // Calculate the new one.
  133.                     X_tmp = star[i].x / star[i].z + star[i].xc;
  134.                 
  135.                     // If x lies within boundaries.
  136.                     if(X_tmp < 320 && X_tmp > 0){
  137.                         Y_tmp = star[i].y / star[i].z + star[i].yc;
  138.                     
  139.                         //if y lies within boundaries.
  140.                         if(Y_tmp < 200 && Y_tmp > 0){
  141.                             star[i].Old_pos = X_tmp + Y_tmp * 320;
  142.                             *(vidmem + star[i].Old_pos) = star[i].z >> 3;
  143.                             star[i].z -= dz;
  144.                         }else{
  145.                             star[i].z = 0;
  146.                             Num_Active--;
  147.                         }
  148.                     }else{
  149.                         star[i].z = 0;
  150.                         Num_Active--;
  151.                     }
  152.                 }else{
  153.                     star[i].z = 0;
  154.                     Num_Active--;
  155.                 }
  156.             }
  157.         }
  158.         
  159.         if(_kbhit())
  160.             switch(_getch()){
  161.                 case 0x1b    :    quit = 1;
  162.                         break;
  163.                 case '+'    :    if( dz+Factor < Max_Dz) dz += Factor;
  164.                         break;
  165.                 case '-'    :    if( dz-Factor > Min_Dz) dz -= Factor;
  166.                         break;
  167.             }
  168.             
  169.             X_count+=2;
  170.             if(X_count >= Max_Rand) X_count = 0;
  171.             Y_count++;
  172.             if(Y_count >= Max_Rand) Y_count = 0;
  173.             
  174.             // This section increase the X and Y radii, giving 
  175.             // ellipses.            
  176.             if(count % 5){
  177.                 Tube_Xr = 20 * Xtable[D_tube_xr] / Max_Rand + 60;
  178.                 Tube_Yr = 20 * Ytable[D_tube_yr] / Max_Rand + 60;
  179.                 if(++D_tube_xr > Max_Rand) D_tube_xr = 0;
  180.                 if(++D_tube_yr > Max_Rand) D_tube_yr = 0;
  181.             }
  182.             
  183.             count++;
  184.     }
  185.     
  186.     setmode03();
  187. }
  188.  
  189.  
  190. void setmode13(void)
  191. {
  192.     __asm{
  193.             mov        ax,13h
  194.             int        10h
  195.     }
  196. }
  197.  
  198. void setmode03(void)
  199. {
  200.     __asm{
  201.             mov        ax,03
  202.             int        10h
  203.     }
  204. }
  205.  
  206. void waitvrt(void)
  207. {
  208.         __asm{
  209.             mov    dx,3dah
  210.     VRT:
  211.             in        al,dx
  212.             test    al,8
  213.             jnz        VRT
  214.     NoVRT:
  215.             in        al,dx
  216.             test    al,8
  217.             jz        NoVRT
  218.         }
  219. }
  220.     
  221. typedef struct{
  222.     char r;
  223.     char g;
  224.     char b;
  225. }PAL;
  226.  
  227. void setpal(void)
  228. {
  229.     PAL colors[64];
  230.     int i;
  231.     unsigned tmp;
  232.     
  233.     for(i = 0; i < 64; i++){
  234.         tmp = (unsigned)(pow(64,i/48.0) + .5);
  235.         tmp = (tmp >= 64) ? 63:tmp;
  236.         colors[i].r = tmp;
  237.         colors[i].g = tmp;
  238.         colors[i].b = (tmp < 15) ? 15: (tmp >= 64) ? 63: tmp;
  239.     }
  240.     
  241.     _outp( 0x3c8, 1);
  242.     for(i = 64; i > 0; i--){
  243.         _outp(0x3c9, colors[i-1].r);
  244.         _outp(0x3c9, colors[i-1].g);
  245.         _outp(0x3c9, colors[i-1].b);
  246.     }
  247.  
  248.     _outp(0x3c8,0);    
  249.     _outp(0x3c9, 0);
  250.     _outp(0x3c9, 0);
  251.     _outp(0x3c9, 15);
  252.     
  253. }        
  254.         
  255.                         
  256.         
  257.     
  258.  
  259.     
  260.                         
  261.                 
  262.         
  263.